<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Common\Model\User;
use Common\Model\Goods;
use Common\Model\GoodsList;
use Common\Model\Order;
use Common\Model\OrderList;
use Common\Model\goodsAttr;


//测试控制器
class PersonController extends Controller{
	private $userModel;
	private $goodsModel;
	private $goodsListModel;
	private $orderModel;
	private $orderListModel;
	private $goodsAttrModel;
	
	//构造函数
	public function __init()
	{
		$this->userModel=new User;
		$this->goodsModel=new Goods;
		$this->goodsListModel=new GoodsList;
		$this->orderModel=new Order;
		$this->orderListModel=new OrderList;
		$this->goodsAttrModel=new GoodsAttr;
	}
	


	public function index(){
		// p($_SESSION);
		if($_SESSION['info']){
			// 获取订单列表的信息和订单列表的信息
			$uid=$_SESSION['info']['uid'];
			$orderDataOne=$this->orderModel->where("shop_user_uid={$uid} AND status=1")->orderBy('time','DESC')->get();
			$orderDataTwo=$this->orderModel->where("shop_user_uid={$uid} AND status=2")->count('*');
			$orderDataThree=$this->orderModel->where("shop_user_uid={$uid} AND status=3")->count('*');
			$orderDataFour=$this->orderModel->where("shop_user_uid={$uid} AND status=4")->count('*');
			// p($orderDataOne);
			$orderListData=[];
			foreach ($orderDataOne as $k => $v) {
				$temp=$this->orderListModel->where("shop_order_oid={$v['oid']}")->get();

				foreach ($temp as $kk => $vv) {
					$temp[$kk]['gname']=$this->goodsModel->where("gid={$vv['shop_goods_gid']}")->pluck('gname');
					$temp[$kk]['pic']=$this->goodsModel->where("gid={$vv['shop_goods_gid']}")->pluck('pic');
					$glid=$this->goodsListModel->where("glid={$vv['shop_goods_list_glid']}")->pluck('combine');
					$glidArr=explode(',', $glid);
					foreach ($glidArr as $kkk => $vvv) {
						$temp[$kk]['gtvalue'][]=$this->goodsAttrModel->where("gtid={$vvv}")->pluck('gtvalue');
					}
				}
				$orderListData[$k]=$temp;
			}
			// p($orderListData);
			// ①订单信息的获取和分配变量
			View::with('orderDataTwo',$orderDataTwo);
			View::with('orderDataThree',$orderDataThree);
			View::with('orderDataOne',$orderDataOne);
			View::with('orderListData',$orderListData);

			// ②个人信息的数据
			$userData=$this->userModel->where("uid={$_SESSION['info']['uid']}")->find();
			View::with('userData',$userData);

			View::make();
		}else{

			go(U('Index/index'));
		}		
		
	}


	// ajax提交的用户个人信息设置
	public function oneAjax(){
		$data['username']=Q('post.name');
		$data['address']=Q('post.str');
		$data['telphone']=Q('post.telphone');
		$data['uid']=$_SESSION['info']['uid'];
		$this->userModel->save($data);
		// 尼玛前不能有输出
		echo json_encode($data);

		exit;
	}




	// 取消订单的ajax
	public function twoAjax(){
		$orderOid=Q('post.orderOid');
		$this->orderModel->where("oid={$orderOid}")->update(['status'=>4]);
		echo json_encode('ok');
	}




}